package com.bloom.opensourceframework.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bloom.opensourceframework.R;

import java.util.List;

/**
 * Created by bloom on 2018/5/3.
 */

public class FunctionsAdapter extends RecyclerView.Adapter<FunctionsAdapter.MyViewHold> {

    private List<String> mDatas;
    private Context mContext;

    private ItemClickListener mItemClickListener;

    public void setOnItemClickListener(ItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public interface ItemClickListener {
        void itemClick(String func);
    }

    public FunctionsAdapter(Context context, List<String> datas) {
        this.mDatas = datas;
        this.mContext = context;
    }

    @Override
    public MyViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemContent = LayoutInflater.from(mContext).inflate(R.layout.layout_item, null);
        return new MyViewHold(itemContent);
    }

    @Override
    public void onBindViewHolder(MyViewHold holder, final int position) {
        if (mDatas.size() > 0) {
            holder.tvTitle.setText(mDatas.get(position));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mItemClickListener.itemClick(mDatas.get(position));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    class MyViewHold extends RecyclerView.ViewHolder {
        private TextView tvTitle;

        public MyViewHold(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
        }
    }


}
